<?php

namespace app\admin\model\classroom;

use think\Db;
use think\Model;


class Lesson extends Model
{

    

    

    // 表名
    protected $name = 'classroom_lesson';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'status_text',
        'time_up_text',
        'up_time_text',
        'file_type_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
        self::afterDelete(function ($row) {
            Db::name("classroom_course_msg")->where("lesson_id",$row['id'])->delete();
            Db::name("classroom_course_record")->where("lesson_id",$row['id'])->delete();
            Db::name("classroom_collect")->where("lesson_id",$row['id'])->delete();
        });
    }

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }

    public function getTimeUpList()
    {
        return ['0' => __('Time_up 0'), '1' => __('Time_up 1')];
    }
    
    public function getFileTypeList()
    {
        return ['1' => __('File_type 1'), '2' => __('File_type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTimeUpTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time_up']) ? $data['time_up'] : '');
        $list = $this->getTimeUpList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUpTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['up_time']) ? $data['up_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUpTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    
    public function getFileTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flie_type']) ? $data['flie_type'] : '');
        $list = $this->getFileTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function course()
    {
        return $this->belongsTo('Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}